package com.bodu.global.config.handler;

import cn.hutool.json.JSONUtil;
import com.bodu.core.entity.result.RStateEnum;
import com.bodu.core.entity.result.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author gaoya
 * @Description 登录失败
 * @date 2022 - 10 - 13 00:17:46
 */
@Component
public class FailHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        httpServletResponse.setStatus(RStateEnum.SUCCESS.getCode());
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().print(JSONUtil.parse(Result.fail(RStateEnum.NO_AUTH.getCode(), "登录失败！")));
    }
}
